﻿//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Mapping Application Block
//===============================================================================

using System;
using System.Collections.Generic;
using EntLibContrib.Query;

namespace EntLibContrib.Mapping.QuickStart.DomainObjects
{
	public abstract class DomainBase
	{
		#region Fields
		private Guid id;
		private Guid identityId;
		private Audit audit;
		private bool isDirty;
		#endregion

		#region Properties
		[Map()]
		public Guid Id
		{
			get { return id; }
			set
			{
				id = value;
				isDirty = true;
			}
		}

		[Map()]
		public Guid IdentityId
		{
			get { return identityId; }
			set
			{
				identityId = value;
				isDirty = true;
			}
		}

		public Audit Audit
		{
			get
			{
				if (audit == null)
				{
					if (id != Guid.Empty)
					{
						IDictionary<string, object> parameters = new Dictionary<string, object>();
						parameters.Add("PrimaryId", id);
						parameters.Add("SecondaryId", id);
						audit = MapperFactory.CreateMapper("Audit Mapper").ToDomainObject<Audit>(QueryFactory.CreateQuery("Read Audit By Id").ExecuteForRead(parameters));
					}
				}

				return audit;
			}
			set
			{
				audit = value;
				id = audit.PrimaryId;
			}
		}

		public bool IsDirty
		{
			get { return isDirty; }
			set { isDirty = value; }
		}
		#endregion

		#region Construction
		protected DomainBase()
		{ }
		#endregion
	}
}
